import Vue from 'vue'
import App from './App.vue'
import routes from "@/router"
import VueRouter from "vue-router";

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false;
Vue.use(ElementUI);


Vue.use(VueRouter);

const { name } = require('../package.json');

let router = null;
let instance = null;

function render(props = {}){
  //主应用传递过来的挂载子应用的div容器
  const { container } = props;

  //创建router
  router = new VueRouter({
    base: window.__POWERED_BY_QIANKUN__ ? `/${name}` : "/",
    mode: "history",
    routes
  });

  instance = new Vue({
    render: h => h(App),
    router
  }).$mount(container ? container.querySelector('#app') : '#app') //避免和主应用id冲突
}

//独立运行时
if(!window.__POWERED_BY_QIANKUN__){
  render();
}

export async function bootstrap(){
  console.log('vue2 子应用 bootstrap');
}

export async function mount(props){
  console.log('vue2 子应用 mount');
  render(props);
}

export async function unmount(){
  console.log('vue2 子应用 unmount');
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
  router = null;
}
